package com.fh.alipay.controller;

import com.fh.alipay.vo.ApiResult;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import com.fh.alipay.po.GoodsImg;
import com.fh.alipay.service.IGoodsImgService;
import com.fh.alipay.model.dto.GoodsImgDTO;
import com.fh.alipay.model.vo.GoodsImgVO;
import com.fh.alipay.model.query.GoodsImgQuery;
import com.fh.alipay.model.convert.GoodsImgConvert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Arrays;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author fh
 * @date 2022-05-03
 * @description 商品图片表控制器
 */
@RestController
@RequestMapping("/aliPay/goodsImg")
public class GoodsImgController extends BaseController {

    @Resource
    private IGoodsImgService goodsImgService;


    /**
     * 获取商品图片表分页列表
     */
    @PostMapping(value = "/list")
    public ApiResult<IPage<GoodsImg>> list(GoodsImgQuery query, Page page) {
        IPage<GoodsImg> resultPage = goodsImgService.getWithPage(page, query);
        return ApiResult.ok(resultPage);
    }

    /**
     * 根据ID获取商品图片表详情
     */
    @GetMapping("/get")
    public ApiResult<GoodsImgVO> get(@RequestParam("id") Long id) {
        GoodsImg entity = goodsImgService.getById(id);
        GoodsImgVO vo = GoodsImgConvert.toVO(entity);
        return ApiResult.ok(vo);
    }

    /**
     * 新增商品图片表
     *
     * @return
     */
    @PostMapping("/add")
    public ApiResult add(MultipartFile imgFile) {
        return goodsImgService.add(imgFile);
    }

    /**
     * 编辑商品图片表
     */
    @PostMapping("/update")
    public ApiResult update(@RequestBody GoodsImgDTO dto) {
        GoodsImg po = GoodsImgConvert.toPO(dto);
        po.setUpdateTime(LocalDateTime.now());
        boolean update = goodsImgService.updateById(po);
        return update ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 移除商品图片表
     */
    @PostMapping("/remove")
    public ApiResult<String> remove(@RequestParam(value = "id") Long id) {
        boolean remove = goodsImgService.removeById(id);
        return remove ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 批量移除商品图片表
     */
    @PostMapping("/batch/remove")
    public ApiResult batchRemove(@RequestParam(value = "ids") String ids) {
        boolean remove = goodsImgService.removeByIds(Arrays.asList(ids.split(",")));
        return remove ? ApiResult.ok() : ApiResult.fail();
    }
}
